package com.newland.esp.controller.goods;

import com.newland.esp.common.util.DataResult;
import com.newland.esp.service.goods.FileUploadService;
import com.newland.esp.vo.response.goods.FileResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @param
 * @author lw
 * @descripe
 * @date 2021/10/30 18:19
 * @Version 1.0
 **/
@RestController
@RequestMapping("/fileUpload")
public class FileUploadController {
    private static Logger logger = LoggerFactory.getLogger(FileUploadController.class);
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传文件至 ftp 服务器
     */
    @RequestMapping("/save")
    public DataResult fileUploadSave(MultipartFile file) {
        try {
            return fileUploadService.fileUpload(file.getOriginalFilename(),
                    file.getInputStream());
        } catch (IOException e) {
            logger.error("文件上传失败： " + e.getMessage());
        }
        return null;
    }
    /**
     * 删除 ftp 服务器上的图片
     */
    @RequestMapping("/delete")
    public DataResult fileDelete(String pathName) {
            boolean result = fileUploadService.fileDelete(pathName);
        if(result == true){
            return DataResult.success("删除图片成功",null);
        }
        return DataResult.error();
    }


}
